package com.googlecode.imanager.swing.util;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;

import com.googlecode.imanager.client.bridge.style.IBorder;
import com.googlecode.imanager.client.style.BorderStyle;
import com.googlecode.imanager.core.base.style.ComputedStyle;

/**
 * @author wellsantos@wellsantos.com
 * @since 0.01
 */
public class SwingBorder implements javax.swing.border.Border {

    private ComputedStyle style;
    private Color leftColor;
    private Color topColor;
    private Color rightColor;
    private Color bottomColor;
    
    public SwingBorder(ComputedStyle style) {
        this.style = style;
    }
    
    public boolean isEmpty(){
        return (style.getBorderTop().getInset() 
              | style.getBorderLeft().getInset()
              | style.getBorderBottom().getInset()
              | style.getBorderRight().getInset()) == 0;
    }
    
    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(
                style.getBorderTop().getInset(), 
                style.getBorderLeft().getInset(),
                style.getBorderBottom().getInset(),
                style.getBorderRight().getInset());
    }

    @Override
    public boolean isBorderOpaque() {
        return isOpaque(style.getBorderLeft())
            || isOpaque(style.getBorderTop())
            || isOpaque(style.getBorderRight())
            || isOpaque(style.getBorderBottom());
    }
    
    private boolean isOpaque(IBorder b){
        return b != null 
            && b.getStyle() != null
            && b.getStyle() != BorderStyle.SOLID;
    }

    @Override
    public void paintBorder(Component component, Graphics g, int x, int y, int width, int height) {
        IBorder b;
        int cor;
        int len;
        
        width--;
        height--;
        g.setPaintMode();
        
        // left
        
        b = style.getBorderLeft();
        len = b.getInset();
        cor = b.getColor() == null ? 0 : b.getColor().getRGB();
        
        if (len > 0){
            Color c = leftColor;
            if (c == null ||
                    c.getRGB() != cor){
                c = new Color(cor);
                leftColor = c;
            }
            
            g.setColor(c);
            g.fillRect(x, y, x+len, y+height);
        }
        
        // right

        b = style.getBorderRight();
        len = b.getInset();
        cor = b.getColor() == null ? 0 : b.getColor().getRGB();
        
        if (len > 0){
            Color c = rightColor;
            if (c == null ||
                    c.getRGB() != cor){
                c = new Color(cor);
                rightColor = c;
            }
            
            g.setColor(c);
            g.fillRect(x+width-len+1, y, x+width, y+height);
        }
        
        // bottom

        b = style.getBorderBottom();
        len = b.getInset();
        cor = b.getColor() == null ? 0 : b.getColor().getRGB();
        
        if (len > 0){
            Color c = bottomColor;
            if (c == null ||
                    c.getRGB() != cor){
                c = new Color(cor);
                bottomColor = c;
            }
            
            g.setColor(c);
            g.fillRect(x, y+height-len+1, x+width, y+height);
        }

        // top

        b = style.getBorderTop();
        len = b.getInset();
        cor = b.getColor() == null ? 0 : b.getColor().getRGB();
        
        if (len > 0){
            Color c = topColor;
            if (c == null ||
                    c.getRGB() != cor){
                c = new Color(cor);
                topColor = c;
            }
            
            g.setColor(c);
            g.fillRect(x, y, x+width, y+len);
        }
        
    }

}
